/*
 * net_opration.cpp
 *
 *  Created on: 2012-7-5
 *      Author: ThinkPad
 */

#include "net_opration.h"

net_opration::net_opration(void)
{
        m_count = 0;
        op = _NULL;
        op_queue_size = DFLT_MIN_CONN_SIZ;
}

net_opration::~net_opration(void)
{
    op_lock.Lock();
    {
            while(free_opration.size()>0)
            {
                    io_opration * op  = free_opration.top();
                    free_opration.pop();

                    if(op != NULL)
                    {
                            op->token = NULL;
                            delete op;
                    }
            }

            while(queue_opration.size()>0)
            {
                    io_opration * op  = queue_opration.front();
                    queue_opration.pop();
                    if(op != NULL)
                    {
                            op->token = NULL;
                            delete op;
                    }
            }
            op_lock.Unlock();
    }
}

/*
*
*init event queue
*/
int net_opration::init_queue(int size)
{
	/**
	 * init size
	 */
	op_queue_size = size;

	/**
	 * init buf
	 */
    op_lock.Lock();
    {
            for(int i=0;i < op_queue_size;i++)
            {
                    /*io event*/
                    io_opration *new_op = new io_opration();
                    if(new_op == _NULL)
                    {
                            op_lock.Unlock();
                            return FAILURE;
                    }

                    free_opration.push(new_op);
            }
            op_lock.Unlock();
    }
    return SUCCESS;
}

/*
*event enter queue
*/
int net_opration::op_enqueue(NETPROTOCL net_protocl,user_token * token)
{
    if(token == _NULL)
            return FAILURE;

    op_lock.Lock();
    {
            if(free_opration.size() == 0)
            {
                    op = new io_opration();
            }
            else
            {
                    op = free_opration.top();
                    free_opration.pop();
            }
            if(op == _NULL)
            {
                    op_lock.Unlock();
                    return FAILURE;
            }
            op->net_protocl = net_protocl;
            op->token = token;

            queue_opration.push(op);

            m_count++;

            op_lock.Unlock();
    }
    return SUCCESS;
}

/*
*
*event delete queue
*/
int net_opration::op_dequeue(NETPROTOCL *net_protocl,user_token ** token)
{
    op_lock.Lock();
    {
            if(m_count == 0)
            {
                    op_lock.Unlock();
                    return FAILURE;
            }

            op = queue_opration.front();
            queue_opration.pop();
            if(op == _NULL)
            {
                    op_lock.Unlock();
                    return FAILURE;
            }

            m_count--;
            *net_protocl = op->net_protocl;
            *token = op->token;

            /*return to free pool*/
            free_opration.push(op);

            op_lock.Unlock();
    }
    return SUCCESS;
}
/*
*current event count
*/
int net_opration::get_opration_count()
{
    return m_count;
}

